import RobotError from "../../errors/robot-error.js";
import Goods from "../../shop/goods/goods.js";
import Ability, { CoinHolder, ItemHolder } from "../ability.js";
import Member from "./member.js";


interface P extends CoinHolder, ItemHolder { }

/**购买商品 */
export default class PurchaseM extends Member {
	host: P;

	constructor(host: P) {
		super(host);
	}

	/**购买 */
	public purchase(goods: Goods, num: number = 1) {
		if (!this.canBuy(goods, num)) throw new RobotError("硬币不足");
		this.host.coin -= goods.price * num;
		this.host.addItem(goods.item, num);
		return {
			msg: `购买成功，共花费${goods.price * num}硬币\n` +
				`你现在还有${this.host.coin}硬币。`
		};
	}

	/**判断所持有的硬币是否足够购买 */
	canBuy(goods: Goods, num: number = 1) {
		return goods.price * num <= this.host.coin;
	}

}